<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Session;
class Field extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $model = $this->getModel();
        
        // 列表过滤器，生成查询Map对象
        $map = $this->search($model, [$this->fieldIsDelete => $this::$isdelete]);
        if ($this->request->param('table_name')) {
            $map['table_name'] = ["like", "%" . $this->request->param('table_name') . "%"];
        }
		if ($this->request->param('field_name')) {
            $map['field_name'] = ["like", "%" . $this->request->param('field_name') . "%"];
        }
        // 特殊过滤器，后缀是方法名的
        $actionFilter = 'filter' . $this->request->action();
        if (method_exists($this, $actionFilter)) {
            $this->$actionFilter($map);
        }
        
        // 自定义过滤器
        if (method_exists($this, 'filter')) {
            $this->filter($map);
        }
        $table_name = $this->request->param('table_name');
        $field_name = $this->request->param('field_name');
        $this->getTable($table_name,$field_name);
        $this->datalist($model, $map);
        return $this->view->fetch();
    }
    /**
     * 获取数据库中的所有数据表
     */
    public function getTable($table_name, $field_name) {
        $model = Db::name("field");
        $table_list = $model->query("select TABLE_NAME name
									   from information_schema.TABLES
									  where TABLE_SCHEMA = 'teacher_ethics'
									  order by name");
        $this->view->assign("table_list", $table_list);
        //查询当前数据表的所有字段（列）
        $field_list = $model->query("select COLUMN_NAME name
										  from information_schema.COLUMNS
										 where TABLE_SCHEMA = 'teacher_ethics'
										   and TABLE_NAME = '".$table_name[0]."'") ;
        $this->view->assign("field_list", $field_list);
    }
    /**
     * 获取数据表中的的字段（异步请求时调用）
     */
    public function getColumn() {
        $table_name = $this->request->param('table_name');
        if(!empty($table_name)) {
            $model = Db::name("field");
            $data = $model->query("select COLUMN_NAME name
									 from information_schema.COLUMNS
									where TABLE_SCHEMA = 'teacher_ethics'
									  and TABLE_NAME = '".$table_name."'" );
            if(!empty($data)) {
                //$this->ajaxReturn($data, "查询成功", 1);
                return  ['data'=>$data,'status'=>1];
            } else {
                //$this->ajaxReturn ( null, "没有找到对应的字段", 0 );
                return  ['data'=>null,'status'=>0];
            }
        } else {
            return false;
        }
    }
    protected function beforeAdd()
    {
        $id=$this->request->param('id');
        $data=Db::name('field')->where('id',$id)->find();
        $model = Db::name("field");
        $table_list = $model->query("select TABLE_NAME name
									   from information_schema.TABLES
									  where TABLE_SCHEMA = 'teacher_ethics'
									  order by name");
        $this->view->assign("table_list", $table_list);
        //dump($table_list);
        $field_list = $model->query("select COLUMN_NAME name
										  from information_schema.COLUMNS
										 where TABLE_SCHEMA = 'teacher_ethics'
										   and TABLE_NAME = '".$data['table_name']."'") ;
        $this->view->assign("field_list", $field_list);
		$operator = Session::get('user_name');
		$this->view->assign("operator", $operator);
        $this->view->assign("data", $data);
    }
    protected function beforeEdit()
    {
        $id=$this->request->param('id');
        $data=Db::name('field')->where('id',$id)->find();
//         dump($data);
        $model = Db::name("field");
        $table_list = $model->query("select TABLE_NAME name
									   from information_schema.TABLES
									  where TABLE_SCHEMA = 'teacher_ethics'
									  order by name");
        //查询当前数据表的所有字段（列）
        $field_list = $model->query("select COLUMN_NAME name
										  from information_schema.COLUMNS
										 where TABLE_SCHEMA = 'teacher_ethics'
										   and TABLE_NAME = '".$data['table_name']."'") ;
        $this->view->assign("field_list", $field_list);
        //dump($field_list);
        $this->view->assign("table_list", $table_list);
        $this->view->assign("data", $data);
		$operator = Session::get('user_name');
		$this->view->assign("operator", $operator);
    }
}
